package com.qh.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.qh.dao.CommonDao;
import com.qh.util.ConvertUtil;
import com.qh.util.SqlUtil;

@Service
public class CommonServiceImpl {

	@Resource
	private CommonDao dao;

	public List<Map<String, Object>> getObjectList(String sql) {

		List<Map<String, Object>> objectList = null;

		try {
			objectList = this.dao.select(sql);
		} catch (Exception e) {
			objectList = new ArrayList<>();
			System.err.println("CommonServiceImpl.getObjectList() Exception" + e.getMessage());
		}

		return objectList;
	}

	public Map<String, Object> getObjectList(Map<String, String> parameterMap) {

		Map<String, Object> resultMap = new HashMap<>();

		String sqlID = parameterMap.get("queryID");
		String[] arguments = parameterMap.get("condition").split(",");

		String sql = SqlUtil.getProperty(sqlID, arguments);
		List<Map<String, Object>> objectList = this.getObjectList(sql);
		
		resultMap.put("count", objectList.size());
		resultMap.put("dataList", objectList);

		return resultMap;
	}

	public Map<String, String> save(Map<String, String> parameterMap) {

		Map<String, String> resultMap = new HashMap<>();
		resultMap.put("code", "0");

		String table = parameterMap.get("table");
		String id = parameterMap.get("id");

		StringBuffer sql = new StringBuffer("");
		if (ConvertUtil.isEmpty(id)) {

			sql.append("insert into ");
			sql.append(table);
			sql.append("(");

			StringBuffer columns = new StringBuffer("");
			StringBuffer values = new StringBuffer("");

			for (Entry<String, String> entry : parameterMap.entrySet()) {

				String key = entry.getKey();
				String value = entry.getValue();

				if (!"table".equals(key) && !"id".equals(key)) {

					if (columns.length() == 0) {
						columns.append(key);
					} else {
						columns.append(", ");
						columns.append(key);
					}

					if (values.length() == 0) {
						values.append("'");
						values.append(value);
						values.append("'");
					} else {
						values.append(", '");
						values.append(value);
						values.append("'");
					}
				}
			}

			sql.append(columns.toString());
			sql.append(") ");

			sql.append("values(");
			sql.append(values.toString());
			sql.append(")");
		} else {

			boolean isEnd = false;

			sql.append("update ");
			sql.append(table);
			sql.append(" set ");

			for (Entry<String, String> entry : parameterMap.entrySet()) {

				String key = entry.getKey();
				String value = entry.getValue();

				if (!"table".equals(key) && !"id".equals(key)) {

					if (!isEnd) {
						
						isEnd = true;
						
						sql.append(key);
						sql.append(" = '");
						sql.append(value);
						sql.append("'");
					} else {

						sql.append(", ");
						sql.append(key);
						sql.append(" = '");
						sql.append(value);
						sql.append("'");
					}
				}
			}

			if (id.startsWith("id")) {
				sql.append("where ");
				sql.append(id);
			} else {
				sql.append("where id = ");
				sql.append(id);
			}
		}

		System.out.println("ManagerServiceImpl.save() sql = [" + sql.toString() + "]");

		try {
			int result = 0;
			if (ConvertUtil.isEmpty(id)) {
				result = this.dao.insert(sql.toString());
			} else {
				result = this.dao.update(sql.toString());
			}
			System.out.println("ManagerServiceImpl.save() result = [" + result + "]");
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("code", "1");
		}

		return resultMap;
	}

	public Map<String, String> delete(Map<String, String> parameterMap) {

		Map<String, String> resultMap = new HashMap<>();
		resultMap.put("code", "0");

		StringBuffer sql = new StringBuffer("");
		sql.append("delete from ");
		sql.append(parameterMap.get("table"));
		sql.append(" where 1 = 1 ");

		for (Entry<String, String> entry : parameterMap.entrySet()) {

			String key = entry.getKey();
			String value = entry.getValue();

			if (!"table".equals(key)) {

				sql.append("and ");
				sql.append(key);
				sql.append(" = '");
				sql.append(value);
				sql.append("' ");
			}
		}

		try {
			int result = this.dao.delete(sql.toString());
			System.out.println("ManagerServiceImpl.delete() result = [" + result + "]");
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("code", "1");
		}

		return resultMap;
	}
}
